package httpapi

import (
	"strconv"

	"away.wei/minidevpos/db"
	"github.com/gin-gonic/gin"
)

type ProjectBuildApi struct{}

// @Tags Builder
// @Summary 添加
// @Security ApiKeyAuth
// @Param data body []db.ProjectBuild  true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /project/builder [post]
func (s *ProjectBuildApi) Create(c *gin.Context) {
	var dtos []db.ProjectBuild
	c.ShouldBindJSON(&dtos)
	if err := projectBuildRep.Create(dtos); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("创建成功", c)
	}
}

// @Tags Builder
// @Summary 删除
// @Security ApiKeyAuth
// @Param ids body []int true "编号"
// @Router /project/builder [delete]
func (s *ProjectBuildApi) Delete(c *gin.Context) {
	var ids []int
	c.ShouldBindJSON(&ids)
	if err := projectBuildRep.Delete(ids); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithMessage("删除成功", c)
	}
}

// @Tags Builder
// @Summary 更新
// @Security ApiKeyAuth
// @Param data body []db.ProjectBuild  true " "
// @Router /project/builder [put]
func (s *ProjectBuildApi) Update(c *gin.Context) {
	var dtos []db.ProjectBuild
	c.ShouldBindJSON(&dtos)
	if errs := projectBuildRep.Update(dtos); len(errs) != 0 {
		FailWithDetailed(errs, "更新失败", c)
	} else {
		OkWithMessage("更新成功", c)
	}
}

// @Tags Builder
// @Summary 查询脚本
// @Security ApiKeyAuth
// @Param project_id query int true "项目编号"
// @Router /project/builder [get]
func (s *ProjectBuildApi) GetProjectId(c *gin.Context) {
	projectId, _ := strconv.Atoi(c.Query("project_id"))
	if dtos, err := projectBuildRep.GetProjectId(projectId); err != nil {
		FailWithMessage(err.Error(), c)
	} else {
		OkWithDetailed(dtos, "查询成功", c)
	}
}
